//
//  RHDeviceItemDataCell.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/7/27.
//


#import "RHDeviceItemDataCell.h"
#import "FieldsCellItemView.h"

@interface RHDeviceItemDataCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic ,copy) NSString *deviceTypeCode;

@property (nonatomic ,copy) NSArray<DeviceChartFieldModel *> *formFields;

@property (nonatomic ,strong) UIView *bgBox;

@property (nonatomic ,strong) UICollectionView *collectView;


@end

@implementation RHDeviceItemDataCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    _bgBox = [[UIView alloc] init];
    [_bgBox setBackgroundColor:UIColor.whiteColor];
    [_bgBox.layer setCornerRadius:8];
    [self.contentView addSubview:_bgBox];
//    [_bgBox shadow:UIColor.blackColor opacity:0.1 radius:5 offset:CGSizeMake(0, 1)];
    
    [_bgBox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));
    }];
 
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    _collectView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectView.bounces = NO;
    _collectView.layer.cornerRadius = 8;
    [_collectView setBottomBorder];
    _collectView.backgroundColor = [UIColor whiteColor];
    [_collectView registerClass:[FieldsCellItemView class] forCellWithReuseIdentifier:@"FieldsCellItemView"];
    _collectView.delegate = self;
    _collectView.dataSource = self;
    _collectView.showsVerticalScrollIndicator = NO;
    _collectView.showsHorizontalScrollIndicator = NO;
    _collectView.scrollEnabled = NO;
    [_bgBox addSubview:_collectView];
    [_collectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
        make.height.offset(0);
    }];
    
}

- (void)setFormFields:(NSArray *)formFields deviceTypeCode:(NSString*)deviceTypeCode
{
    _formFields = formFields;
    _deviceTypeCode = deviceTypeCode;
    [_collectView reloadData];
    [_collectView mas_updateConstraints:^(MASConstraintMaker *make) {
        NSInteger row = formFields.count%3 ? formFields.count/3+1 : formFields.count/3;
        if(row>2) row = 2;
        if(row == 0){
            make.height.offset(0);
        }else{
            //最多只显示6个
            make.height.offset(row*70 + 20 );
        }
    }];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.formFields.count>6?6:self.formFields.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FieldsCellItemView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FieldsCellItemView" forIndexPath:indexPath];
    [cell setModel:self.formFields[indexPath.item] deviceTypeCode:self.deviceTypeCode];
    [cell setIndex:indexPath.item totalCount:self.formFields.count>6?6:self.formFields.count];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((kUI_WINDOW_W-30)/3.0-0.1, 70);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 0, 10, 0);
}

@end
